package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.model.dto.FlowCountDTO;
import com.block.common.service.IMerchantCommonService;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDataPushFlowCountInfo;
import com.block.db.mapper.MerchantBaseInfoMapper;
import com.block.db.mapper.MerchantDataPushFlowCountInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

@Service
public class MerchantCommonService implements IMerchantCommonService {

    @Resource
    MerchantBaseInfoMapper merchantBaseInfoMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate<String, FlowCountDTO> redisTemplate;
    @Autowired
    RedisTemplate<String, MerchantBaseInfo> merchantBaseRedisTemplate;

    @Override
    public MerchantBaseInfo getMerchantBaseInfo(Integer infoId) {
        String redisKey = StrUtil.format(RedisKeyConstant.MERCHANT_BASE_INFO_KEY, infoId);
        MerchantBaseInfo merchantBaseInfo = merchantBaseRedisTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isEmpty(merchantBaseInfo)){
            merchantBaseInfo = merchantBaseInfoMapper.selectById(infoId);
            merchantBaseRedisTemplate.opsForValue().set(redisKey, merchantBaseInfo, 7, TimeUnit.DAYS);
        }
        return merchantBaseInfo;
    }

    @Override
    public Integer  getMerchantPushCountInfo(Integer taskId) {

        String nowdate = LocalDate.now().toString();
        String redisKey = StrUtil.format(RedisKeyConstant.MERCHANT_TASK_PUSH_COUNT_KEY, nowdate, taskId);
        String pushNumber = stringRedisTemplate.opsForValue().get(redisKey);

        try {
            if (StrUtil.isNotEmpty(pushNumber)){
                return Integer.valueOf(pushNumber);
            }
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return 0;
    }

    @Resource
    MerchantDataPushFlowCountInfoMapper merchantDataPushFlowCountInfoMapper;

    @Override
    public FlowCountDTO getCacheFlowCount(Integer infoId){
        String redisKey = StrUtil.format(RedisKeyConstant.DATA_PUSH_FLOW_COUNT_KEY, infoId);

        FlowCountDTO flowCountDTO = redisTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isEmpty(flowCountDTO)){

            MerchantDataPushFlowCountInfo flowCountInfo = merchantDataPushFlowCountInfoMapper.getEntityByInfoIdAndDateInfo(infoId);

            if (ObjectUtil.isNotEmpty(flowCountInfo)){
                // 更新，缓存信息
                flowCountDTO = new FlowCountDTO();
                flowCountDTO.setInfoId(flowCountDTO.getInfoId());
                flowCountDTO.setAppNumber(flowCountInfo.getAppNumber());
                flowCountDTO.setH5Number(flowCountInfo.getH5Number());
                flowCountDTO.setApibNumber(flowCountInfo.getApibNumber());
                flowCountDTO.setApisNumber(flowCountInfo.getApisNumber());

                redisTemplate.opsForValue().set(redisKey, flowCountDTO, 3, TimeUnit.DAYS);
            }

        }
        return flowCountDTO;
    }

}
